package com.example.myweb.Controller.Auth;

import com.example.myweb.Common.ApiResponse;
import com.example.myweb.Domain.Entity.Dynamic;
import com.example.myweb.Domain.Entity.User;
import com.example.myweb.Dto.CreateDynamicRequestDTO;
import com.example.myweb.Dto.DynamicDetailDTO;
import com.example.myweb.Service.Auth.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/dynamics")
public class DynamicController {

    @Autowired
    private DynamicService dynamicService;

    /**
     * 创建动态接口
     * POST /dynamics
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> createDynamic(
            @Valid @RequestBody CreateDynamicRequestDTO request,
            @AuthenticationPrincipal User currentUser) {

        Dynamic createdDynamic = dynamicService.createDynamic(request, currentUser);
        Map<String, Object> responseData = Map.of("dynamic_id", createdDynamic.getDynamicId());
        return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.success(responseData, "动态发布成功。"));
    }

    /**
     * 获取单个动态详情
     * GET /dynamics/{id}
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<DynamicDetailDTO>> getDynamicById(
            @PathVariable Long id,
            // === 补上缺失的 currentUser 参数 ===
            @AuthenticationPrincipal User currentUser) {

        // 将 currentUser 传递给 service
        DynamicDetailDTO dynamicDetail = dynamicService.getDynamicById(id, currentUser);
        return ResponseEntity.ok(ApiResponse.success(dynamicDetail, "动态详情获取成功。"));
    }

    /**
     * 获取动态列表（分页）
     * GET /dynamics
     */
    @GetMapping
    public ResponseEntity<ApiResponse<Page<DynamicDetailDTO>>> getAllDynamics(
            Pageable pageable, // Spring会自动注入默认的或前端传入的分页参数
            // === 补上缺失的 currentUser 参数 ===
            @AuthenticationPrincipal User currentUser) {

        // 将 currentUser 传递给 service
        Page<DynamicDetailDTO> dynamicPage = dynamicService.getAllDynamics(pageable, currentUser);
        return ResponseEntity.ok(ApiResponse.success(dynamicPage, "动态列表获取成功。"));
    }

    // ================== 删除过时的 toggleLike 方法 ==================
    /*
    @PostMapping("/{id}/like")
    public ResponseEntity<ApiResponse<Map<String, Boolean>>> toggleLike(...) {
        // 这个方法已被统一的 LikeController 替代，应予删除
    }
    */
    // =============================================================

    /**
     * 删除指定ID的动态
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteDynamic(
            @PathVariable Long id,
            @AuthenticationPrincipal User currentUser) {

        dynamicService.deleteDynamic(id, currentUser);
        return ResponseEntity.ok(ApiResponse.success("动态删除成功"));
    }
//
//    @Autowired
//    private DynamicService dynamicService;
//
//    /**
//     * 创建动态接口
//     * POST /dynamics
//     */
//    @PostMapping
//    public ResponseEntity<ApiResponse<Map<String, Object>>> createDynamic(
//            @Valid @RequestBody CreateDynamicRequestDTO request,
//            @AuthenticationPrincipal User currentUser // 使用 @AuthenticationPrincipal 直接获取当前登录用户，更优雅
//    ) {
//        Dynamic createdDynamic = dynamicService.createDynamic(request, currentUser);
//
//        // 构建一个简单的响应体
//        Map<String, Object> responseData = Map.of(
//                "message", "动态发布成功。",
//                "dynamic_id", createdDynamic.getDynamicId()
//        );
//
//        return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.success(responseData, "动态发布成功。"));
//    }
//
//    /**
//     * 获取单个动态详情
//     * GET /dynamics/{id}
//     */
//    @GetMapping("/{id}")
//    public ResponseEntity<ApiResponse<DynamicDetailDTO>> getDynamicById(@PathVariable Long id) {
//        DynamicDetailDTO dynamicDetail = dynamicService.getDynamicById(id);
//        return ResponseEntity.ok(ApiResponse.success(dynamicDetail, "动态详情获取成功。"));
//    }
//
//    /**
//     * 获取动态列表（分页）
//     * GET /dynamics
//     */
//    @GetMapping
//    public ResponseEntity<ApiResponse<Page<DynamicDetailDTO>>> getAllDynamics(
//            @RequestParam(defaultValue = "0") int page,
//            @RequestParam(defaultValue = "10") int size
//    ) {
//        // 按发布时间倒序排序
//        Pageable pageable = PageRequest.of(page, size, Sort.by("publishTime").descending());
//        Page<DynamicDetailDTO> dynamicPage = dynamicService.getAllDynamics(pageable);
//        return ResponseEntity.ok(ApiResponse.success(dynamicPage, "动态列表获取成功。"));
//    }
//    @PostMapping("/{id}/like")
//    public ResponseEntity<ApiResponse<Map<String, Boolean>>> toggleLike(
//            @PathVariable Long id,
//            @AuthenticationPrincipal User currentUser
//    ) {
//        boolean isLiked = dynamicService.toggleLike(id, currentUser);
//        Map<String, Boolean> response = Map.of("isLiked", isLiked);
//        String message = isLiked ? "点赞成功" : "取消点赞成功";
//        return ResponseEntity.ok(ApiResponse.success(response, message));
//    }
//    /**
//     * 删除指定ID的动态
//     */
//    @DeleteMapping("/{id}")
//    public ResponseEntity<ApiResponse<Void>> deleteDynamic(
//            @PathVariable Long id,
//            @AuthenticationPrincipal User currentUser) {
//
//        dynamicService.deleteDynamic(id, currentUser);
//        return ResponseEntity.ok(ApiResponse.success("动态删除成功"));
//    }
}